package cn.itcast.reggie.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

/**
 * 短信发送工具类
 */
public class SMSUtils {
   /**
    * 发送短信
    * @param signName 签名
    * @param templateCode 模板
    * @param phoneNumbers 手机号
    * @param param 参数
    */
   public static void sendMessage(String signName, String templateCode,String phoneNumbers,String param){
      DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5tAx4JiSX2ab8vpuKgyf", "hpopVLzrX47yfMAVnblmKQjjUlTHfr");//自己账号的AccessKey信息
      IAcsClient client = new DefaultAcsClient(profile);

      CommonRequest request = new CommonRequest();
      request.setSysMethod(MethodType.POST);
      request.setSysDomain("dysmsapi.aliyuncs.com");//短信服务的服务接入地址
      request.setSysVersion("2017-05-25");//API的版本号
      request.setSysAction("SendSms");//API的名称
      request.putQueryParameter("PhoneNumbers", phoneNumbers);//接收短信的手机号码
      request.putQueryParameter("SignName", signName);//短信签名名称
      request.putQueryParameter("TemplateCode", templateCode);//短信模板ID
      //{"code":"param"}
      request.putQueryParameter("TemplateParam", "{\"code\":\""+param+"\"}");//短信模板变量对应的实际值
      try {
         CommonResponse response = client.getCommonResponse(request);
         System.out.println(response.getData());
      } catch (ServerException e) {
         e.printStackTrace();
      } catch (ClientException e) {
         e.printStackTrace();
      }
   }

}